package com.wnn.demo.model.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.wnn.demo.domain.Department;
import com.wnn.demo.model.hospital.service.DepartmentService;
import com.wnn.demo.pojo.Result;
import com.wnn.demo.pojo.ResultCode;
import com.wnn.demo.vo.DeptTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 部门管理
 * @author asus
 */
@RestController
@RequestMapping("/manage/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    /**
     * 获取所有部门-科室的dtree树
     * @param hospitalId  医院id , 只填写医院ID获取该医院
     * @param employeeId
     * @param isCheckArr  参数 “1” 结合医院id一起填写，查询该医院下拥有的部门
     * @param isNotChoose 参数yes 获取的树结构全部未选中
     * @return
     */
    @GetMapping("/findDeptTreeList")
    public Result selectDeptTreeList(String hospitalId ,String employeeId,String isCheckArr,String isNotChoose){
        List< DeptTreeVo >  list = departmentService.selectDeptTreeList(hospitalId ,employeeId ,isCheckArr, isNotChoose);
        return new Result(ResultCode.OK, list);
    }

    /**
     *  给医院分配拥有的部门
     */
    @PostMapping("/updateHospitalHaveDeptList")
    public Result updateHospitalHaveDeptList(String hospitalId,String deptIds){
        int i = departmentService.updateHospitalHaveDeptList(hospitalId,deptIds);
        return new Result(ResultCode.OK);
    }

    @GetMapping("/selectDepartmentAll")
    public Result selectDepartmentAll(Integer type,@RequestParam(defaultValue = "1")Integer page,
                                    @RequestParam(defaultValue = "10")Integer limit){
        PageInfo< List > pageInfo  = departmentService.selectDepartmentAll(type,page,limit);
        return new Result(ResultCode.OK,pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 查找父级部门
     * @return
     */
    @GetMapping("/findParentDept")
    public Result findParentDept(){
        List<Department> list = departmentService.findParentDept();
        return new Result(ResultCode.OK ,list);
    }

    @GetMapping("/findDeptTree")
    public Result findDeptTree(){
        List<Map> list = departmentService.findDeptTree();
        return new Result(ResultCode.OK ,list);
    }


    /**
     * 新增部门
     * @param department
     * @return
     */
    @PostMapping()
    public Result insertDepartment(@RequestBody Department department){
        int i = departmentService.insertDepartment(department);
        if (i>0){
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 更新部门
     * @param department
     * @return
     */
    @PutMapping("/{id}")
    public Result updateDepartment(@PathVariable(value = "id")String id,@RequestBody Department department){
        department.setDepartmentId(id);
        int i = departmentService.updateDepartment(department);
        if (i>0){
            return Result.ok();
        }
        return Result.error();
    }


    /**
     * 根据部门Id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable(value = "id") String id) {
        int i = departmentService.deleteDepartmentById(id);
        if (i>0){
            return Result.ok();
        }
        return Result.error();
    }


    @PostMapping("/batchDepartment")
    public Result batchDepartmentByIds(String ids ) {

        int i = departmentService.batchDepartmentByIds(ids);
        if (i>0){
            return Result.ok();
        }
        return Result.error();

    }

}
